import { v4 as uuidv4 } from 'uuid';
import { initialCategories, initialLinks, initialTags } from './initData';

// 定义数据类型
export interface CategoryItem {
  id: string;
  name: string;
  description: string;
}

export interface TagItem {
  id: string;
  name: string;
  description?: string;
}

export interface LinkItem {
  id: string;
  title: string;
  url: string;
  description: string;
  imageUrl: string;
  category: string;
  tag?: string;
  stats?: {
    views: number;
    favorite: boolean;
  };
}

// 存储键名
const STORAGE_KEYS = {
  CATEGORIES: 'devops-nav-categories',
  TAGS: 'devops-nav-tags',
  LINKS: 'devops-nav-links',
};

// 初始化本地存储
export const initializeStorage = () => {
  if (!localStorage.getItem(STORAGE_KEYS.CATEGORIES)) {
    localStorage.setItem(
      STORAGE_KEYS.CATEGORIES,
      JSON.stringify(initialCategories),
    );
  }
  if (!localStorage.getItem(STORAGE_KEYS.TAGS)) {
    localStorage.setItem(STORAGE_KEYS.TAGS, JSON.stringify(initialTags));
  }
  if (!localStorage.getItem(STORAGE_KEYS.LINKS)) {
    localStorage.setItem(STORAGE_KEYS.LINKS, JSON.stringify(initialLinks));
  }
};

// 初始化
initializeStorage();

// Category Management
export const getCategories = (): CategoryItem[] => {
  const data = localStorage.getItem(STORAGE_KEYS.CATEGORIES);
  return data ? JSON.parse(data) : [];
};

export const addCategory = (category: Omit<CategoryItem, 'id'>) => {
  const categories = getCategories();
  const newCategory = {
    ...category,
    id: uuidv4(),
  };
  categories.push(newCategory);
  localStorage.setItem(STORAGE_KEYS.CATEGORIES, JSON.stringify(categories));
  return newCategory;
};

export const updateCategory = (category: CategoryItem) => {
  const categories = getCategories();
  const index = categories.findIndex((c) => c.id === category.id);
  if (index > -1) {
    categories[index] = category;
    localStorage.setItem(STORAGE_KEYS.CATEGORIES, JSON.stringify(categories));
    return category;
  }
  throw new Error('Category not found');
};

export const deleteCategory = (id: string) => {
  const categories = getCategories();
  const newCategories = categories.filter((c) => c.id !== id);
  localStorage.setItem(STORAGE_KEYS.CATEGORIES, JSON.stringify(newCategories));
};

// Tag Management
export const getTags = (): TagItem[] => {
  const data = localStorage.getItem(STORAGE_KEYS.TAGS);
  return data ? JSON.parse(data) : [];
};

export const addTag = (tag: Omit<TagItem, 'id'>) => {
  const tags = getTags();
  const newTag = {
    ...tag,
    id: uuidv4(),
  };
  tags.push(newTag);
  localStorage.setItem(STORAGE_KEYS.TAGS, JSON.stringify(tags));
  return newTag;
};

export const updateTag = (tag: TagItem) => {
  const tags = getTags();
  const index = tags.findIndex((t) => t.id === tag.id);
  if (index > -1) {
    tags[index] = tag;
    localStorage.setItem(STORAGE_KEYS.TAGS, JSON.stringify(tags));
    return tag;
  }
  throw new Error('Tag not found');
};

export const deleteTag = (id: string) => {
  const tags = getTags();
  const newTags = tags.filter((t) => t.id !== id);
  localStorage.setItem(STORAGE_KEYS.TAGS, JSON.stringify(newTags));
};

// Link Management
export const getLinks = (): LinkItem[] => {
  const data = localStorage.getItem(STORAGE_KEYS.LINKS);
  return data ? JSON.parse(data) : [];
};

export const addLink = (link: Omit<LinkItem, 'id' | 'stats'>) => {
  const links = getLinks();
  const newLink = {
    ...link,
    id: uuidv4(),
    stats: {
      views: 0,
      favorite: false,
    },
  };
  links.push(newLink);
  localStorage.setItem(STORAGE_KEYS.LINKS, JSON.stringify(links));
  return newLink;
};

export const updateLink = (link: LinkItem) => {
  const links = getLinks();
  const index = links.findIndex((l) => l.id === link.id);
  if (index > -1) {
    links[index] = {
      ...link,
      stats: links[index].stats,
    };
    localStorage.setItem(STORAGE_KEYS.LINKS, JSON.stringify(links));
    return links[index];
  }
  throw new Error('Link not found');
};

export const deleteLink = (id: string) => {
  const links = getLinks();
  const newLinks = links.filter((l) => l.id !== id);
  localStorage.setItem(STORAGE_KEYS.LINKS, JSON.stringify(newLinks));
};

export const incrementLinkViews = (id: string) => {
  const links = getLinks();
  const index = links.findIndex((l) => l.id === id);
  if (index > -1) {
    links[index].stats = {
      ...links[index].stats,
      views: (links[index].stats?.views || 0) + 1,
    };
    localStorage.setItem(STORAGE_KEYS.LINKS, JSON.stringify(links));
    return links[index];
  }
  throw new Error('Link not found');
};

export const toggleLinkFavorite = (id: string) => {
  const links = getLinks();
  const index = links.findIndex((l) => l.id === id);
  if (index > -1) {
    links[index].stats = {
      ...links[index].stats,
      favorite: !links[index].stats?.favorite,
    };
    localStorage.setItem(STORAGE_KEYS.LINKS, JSON.stringify(links));
    return links[index];
  }
  throw new Error('Link not found');
};
